package com.imti.mall.product.controller;

import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.pms.pojo.PmsProductCategory;
import com.imti.mall.product.service.PmsProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.*;

/**
 * ClassName: PmsProductCategoryController
 * Description:
 * date: 2022/9/12 23:21
 *
 * @author: youhun
 * @version:
 * @since JDK 1.8
 */
@RestController
@Api(tags = "商品分类")
@RequestMapping("/api/v1/product/category")
public class PmsProductCategoryController {

    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;


    @ApiOperation("根据ID添加商品分类数据")
    @PostMapping(value = "/addPmsProductCategory")
    public CommonResult<Integer> addPmsProductCategory(@RequestBody PmsProductCategory pmsProductCategory) {
        Integer req = pmsProductCategoryService.addPmsProductCategory(pmsProductCategory);
        if(req!=null&&req>0){
            return CommonResult.success(req,"添加成功");
        }else {
            return CommonResult.failed("添加失败");
        }

    }

    @ApiOperation("根据ID修改商品分类数据")
    @PostMapping (value = "/updatePmsProductCategory")
    public CommonResult<Integer> updatePmsProductCategory(@RequestBody PmsProductCategory pmsProductCategory) {
        Integer req = pmsProductCategoryService.updatePmsProductCategory(pmsProductCategory);
        if(req!=null&&req>0){
            return CommonResult.success(req,"修改成功");
        }else {
            return CommonResult.failed("修改失败");
        }

    }
    /**
     * 根据ID删除商品分类数据
     * @param proId
     * @return
     */
    @ApiOperation("根据ID删除商品分类数据")
    @GetMapping(value = "/deletePmsProductCategory/{proId}")
    public CommonResult<Integer> detail(@PathVariable Integer proId) {
        Integer req = pmsProductCategoryService.deletePmsProductCategory(proId);
        if(req!=null&&req>0){
            return CommonResult.success(req,"删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }

    }

    /**
     * 查询商品分类列表
     * @param pmsProductCategory
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "查询商品分类")
    @PostMapping(value = "/findPmsProductCategory" )
    public CommonResult findPmsProductCategory
            (PmsProductCategory pmsProductCategory,
             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        pageNum=(pageNum-1)*pageSize;
        Map<String,Object> map =new HashMap<>();
        map.put("pmsProductCategory",pmsProductCategory);
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        List<PmsProductCategory> PmsProductCategoryList = pmsProductCategoryService.findPmsProductCategory(map);
        int total=pmsProductCategoryService.getPmsProductCategory(pmsProductCategory);
        Map<String, Object> data = new HashMap<>();
        data.put("PmsProductCategoryList",PmsProductCategoryList);
        data.put("total",total);
        return CommonResult.success(data);
    }

    /**
     * 查询商品分类详情
     * @param proId
     * @return
     */
    @ApiOperation("根据ID获取商品分类详情")
    @GetMapping(value = "/getPmsProductCategoryById/{proId}")
    public CommonResult<PmsProductCategory> getPmsProductCategoryById(@PathVariable("proId") Integer proId) {
        PmsProductCategory pmsProductCategory = pmsProductCategoryService.getPmsProductCategoryById(proId);
        if(pmsProductCategory!=null){
            return CommonResult.success(pmsProductCategory);
        }else {
            return CommonResult.failed("未查询到信息");
        }

    }


    @ApiOperation(value = "查询商品分类")
    @PostMapping(value = "/findPmsProductCategoryBYLever" )
    public CommonResult findPmsProductCategoryBYLever
            (PmsProductCategory pmsProductCategory,
             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        pageNum=(pageNum-1)*pageSize;
        Map<String,Object> map =new HashMap<>();
        pmsProductCategory.setLevel(0);
        map.put("pmsProductCategory",pmsProductCategory);
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        List<PmsProductCategory> PmsProductCategoryList = pmsProductCategoryService.findPmsProductCategory(map);
         for(int i=0;i<PmsProductCategoryList.size();i++){
             pmsProductCategory.setLevel(1);
             pmsProductCategory.setParentId(PmsProductCategoryList.get(i).getId());
             map.put("pmsProductCategory",pmsProductCategory);
             List<PmsProductCategory> children=pmsProductCategoryService.findPmsProductCategory(map);
             PmsProductCategoryList.get(i).setChildren(children);
         }

        return CommonResult.success(PmsProductCategoryList);
    }


}
